import java.net.*;
import java.io.*;
import java.util.Vector;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Server extends JFrame 
{
	
	ServerSocket ss;
	ServerSocket fs;
	
	
	Vector<Connection> connectionList = new Vector<Connection>();
	Vector<filterConnection> filterConnectionList = new Vector<filterConnection>();
	
	JLabel title = new JLabel("Send message to the server");
	
	public Server()
	{

        System.err.println("Starting Server for client");
        try{ ss = new ServerSocket(4444);}catch(Exception e){}
        
        System.err.println("Starting Server for filter");
        try{ ss = new ServerSocket(1337);}catch(Exception e){}

     
        TimerT tt = new TimerT();
        tt.start();

        TextParser tp = new TextParser();
        tp.start();
        
        FilterParser fp = new FilterParser();
        fp.start();

        setVisible(false);
        setSize(500,200);

        System.err.println("Server Started");
        setTitle("JChat Server");

	}


	public static void main(String[] args) throws IOException 
	{

	    Server s = new Server();
	
	}

	class TimerT extends Thread
	{
    	public void run()
    	{
        	while(true)
        	{

        		System.out.println("acceptor running");

    		try 
    		
    		{
		        Socket clientSocket = null;
		        Socket filterSocket = null;
		        System.out.println("waiting for connection from client" );
		        clientSocket = ss.accept();
		        System.out.println("got connection from client" );
		        
		        System.out.println("waiting for connection from filter" );
		        filterSocket = fs.accept();
		        System.out.println("got connection from filter" );              
		
		        PrintWriter outFilter =new PrintWriter(filterSocket.getOutputStream(), true); 
		        BufferedReader inFilter = new BufferedReader(new InputStreamReader( filterSocket.getInputStream()));
		        
		        
		        
		        PrintWriter out =new PrintWriter(clientSocket.getOutputStream(), true); 
		        BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream()));
		
		
				FileReader read = new FileReader("version.info");
				Scanner readText = new Scanner(read); 
			
				out.println(readText.nextLine());
				out.println(readText.nextLine() + "\n");
				
			/*	FileReader report = new FileReader("C:\\logs\\log.rtf");
				Scanner reportText = new Scanner(report);
				out.println("FROM SERVER>");
				
				
				
						String fromFilter;
						FileReader logReader = new FileReader("C:\\logs\\log.rtf");
						BufferedReader myIn = new BufferedReader(logReader);
						while( (fromFilter = myIn.readLine()) != null)
						{
							out.println(fromFilter);
						
						}			
							
			
	
			
			
			
			
				
				if (reportText.nextLine() != null)
				{
					out.println(reportText.nextLine());
				}
				
				
				while(reportText.hasNextLine()){

				
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine());
				out.println(reportText.nextLine() + "\n");
				
				}
				
				*/
			//	File logFile = new File("C:\\logs\\log.rtf");
			//	logFile.delete();
			//	File newLogFile = new File("C:\\logs\\log.rtf");
			//	newLogFile.createNewFile();
				//newLogFile.close();
					
				
		
		        Connection c = new Connection();
		        c.out = out;
		        c.in = in;
		        c.socket = clientSocket;
		        connectionList.add(c);
		        
		        
		        filterConnection f = new filterConnection();
		        f.out = outFilter;
		        f.in = inFilter;
		        f.socket = filterSocket;
		        filterConnectionList.add(f);
		
       		}
        
        
        catch (Exception e){ System.out.println("error with this client");  e.printStackTrace();}

		try{Thread.sleep(100);} catch(Exception e){System.out.println("sleep erro");}
        
        	}

    	}
	}


	class Connection
	{
	    Socket socket;
	    PrintWriter out;
	    BufferedReader in;

	}
	
	
	class filterConnection
	{
		Socket socket;
		PrintWriter out;
		BufferedReader in;
	
	
	}


	class TextParser extends Thread
	{

        public void run()
        {

                while (true)

                {

                        for(int i = 0; i < connectionList.size(); i++)
                         {

                                try
                                {				
								
                                	System.out.println("reading " + i);
                                	Connection c = connectionList.elementAt(i); 
                            		Thread.sleep(200);

                                	System.out.println("reading " + i);
                                
                                
                               		 String s = "";
                               		 
				                	
                               		 
                                		if (c.in.ready() == true)
										{
											s = c.in.readLine();
											//System.out.println(i + "> "+ s);
											
										     if (s.equals("/quit"))

											{
													System.out.println("quitted");
													 
											}		
																
										
										
										
										if(! s.equals(""))
										{
													
										 	for(int j = 0; j < connectionList.size(); j++)
		                         			{
												Connection c2 = connectionList.elementAt(j);  
		                        				c2.out.println(s);
		                        			}
										}	
										
									}
                        	
                        
                        		
                        		
                                }
                            
                            catch(Exception e){System.out.println("reading error");
                            
                            }
                        }

                }


        }


}

	class FilterParser extends Thread
	{

        public void run()
        {

                while (true)

                {

                        for(int i = 0; i < filterConnectionList.size(); i++)
                         {

                                try
                                {				
								
                                	System.out.println("reading filter " + i);
                                	filterConnection f = filterConnectionList.elementAt(i); 
                            		Thread.sleep(200);

                                	System.out.println("reading filter " + i);
                                
                                
                               		 String fs = "";
                               		 
				                	
                               		 
                                		if (f.in.ready() == true)
										{
											fs = f.in.readLine();
											//System.out.println(i + "> "+ s);
											
										     if (fs.equals("/quit"))

											{
													System.out.println("quitted");
													 
											}		
																
										
										
										
										if(! fs.equals(""))
										{
													
										 	for(int j = 0; j < connectionList.size(); j++)
		                         			{
												filterConnection f2 = filterConnectionList.elementAt(j);  
		                        				f2.out.println(fs);
		                        			}
										}	
										
									}
                        	
                        
                        		
                        		
                                }
                            
                            catch(Exception e){System.out.println("reading error");
                            
                            }
                        }

                }


        }


}





}


